<template>
	<view class="content">
		<DtudyListVue v-for="e,i in studyList" :key="i" :val="e" type="1"></DtudyListVue>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onNavigationBarButtonTap, onLoad, onReachBottom } from '@dcloudio/uni-app'
	import DtudyListVue from '/pages/tabbar/components/study/DtudyList'
	import config from '../../config/index.js'
	
	let AnchorId = ''
	let page = 1
	let limit = 10
	const loadOver = ref(false)
	const studyList = ref([])
	
	onLoad(() => {
		GetAnchorInfo()
		uni['my-lesson.ReloadList'] = ReloadList
	})
	
	onReachBottom(() => {
		GetList()
	})
	
	onNavigationBarButtonTap(e => {
		uni.navigateTo({
			url: `./publish-lesson?id=${AnchorId}`
		})
	})
	
	async function GetAnchorInfo() {
		if (uni.getStorageSync('user').anchor) {
			if (uni.getStorageSync('user').anchor.id) {
				AnchorId = uni.getStorageSync('user').anchor.id
				GetList()
				return
			}
		}
		let {
			data: res
		} = await uni.request({
			url: config.baseUrl + '/app/queryAnchorInfoByMemberId?' + `member.id=${uni.getStorageSync('user').id}`,
			// url: config.baseUrl + '/app/queryAnchorInfoByMemberId?' + `member.id=1348430118212599808`,
			method: 'POST'
		})
		let {
			data,
			code
		} = res
		if (code == 200) {
			AnchorId = data.id
		}
		GetList()
	}
	
	function ReloadList() {
		studyList.value = []
		page = 1
		GetList()
	}
	
	function GetList() {
		if (loadOver.value) return
		uni.request({
			url: config.baseUrl + '/app/anchorGetAddCourselist',
			method: 'POST',
			data: {
				'anchor.id': AnchorId,
				page,
				limit
			},
			header: {
				"Content-Type": "application/x-www-form-urlencoded"
			}
		}).then(res => {
			console.log(res)
			let {
				data: resData
			} = res
			let {
				data
			} = resData
			if (data.length < limit) loadOver.value = true
			page++
			studyList.value.push(...data)
		})
	}
</script>

<style>
	page {
		background-image: none;
	}
	
	.content {
		padding: 20rpx 20rpx 60rpx;
		display: flex;
		flex-direction: column;
		gap: 20rpx;
	}
</style>
